Skip to content

General: Introduce wp_get_branch_version() helper to extract major.minor version safely.#11211

Open
tejas0306 wants to merge 4 commits intoWordPress:trunkfrom
tejas0306:add/wp-get-branch-version
Open

General: Introduce wp_get_branch_version() helper to extract major.minor version safely.#11211
tejas0306 wants to merge 4 commits intoWordPress:trunkfrom
tejas0306:add/wp-get-branch-version

Conversation

@tejas0306
Copy link

This introduces a new helper function wp_get_branch_version() for extracting the WordPress branch version (major.minor) from a version string.

Currently, several different approaches are used across core to determine the branch version, including:

  • (float) casting of get_bloginfo( 'version' )
  • substr( $ver, 0, 3 )
  • explode( '.' ) based parsing
  • preg_split() in class-core-upgrader.php

Some of these approaches are fragile or incorrect:

  • (float) casting can produce incorrect values due to floating-point precision issues.
  • substr( $ver, 0, 3 ) breaks for versions >= 10.
  • Multiple inconsistent approaches make maintenance harder.

This patch introduces wp_get_branch_version() using a string-based approach:

function wp_get_branch_version( $version = '' ) {
    if ( '' === $version ) {
        $version = wp_get_wp_version();
    }

    $parts = preg_split( '/[.-]/', $version, 3 );

    return $parts[0] . '.' . ( $parts[1] ?? '0' );
}

Trac ticket: https://core.trac.wordpress.org/ticket/64830

@github-actions
Copy link

github-actions bot commented Mar 9, 2026

Hi @tejas0306! 👋

Thank you for your contribution to WordPress! 💖

It looks like this is your first pull request to wordpress-develop. Here are a few things to be aware of that may help you out!

No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description.

Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making.

More information about how GitHub pull requests can be used to contribute to WordPress can be found in the Core Handbook.

Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook.

If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook.

The Developer Hub also documents the various coding standards that are followed:

Thank you,
The WordPress Project

@github-actions
Copy link

github-actions bot commented Mar 9, 2026

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props suhan2411, siliconforks, mukesh27, peterwilsoncc.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions
Copy link

github-actions bot commented Mar 9, 2026

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Comment on lines +8989 to +8991
}

if ( 'minor' === $part ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
if ( 'minor' === $part ) {
} elseif ( 'minor' === $part ) {

* current WordPress version from wp_get_wp_version().
* @return string The major version string in "x.y" format (e.g. "7.0").
*/
function wp_get_branch_version( $version = '' ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is required, the need in core isn't wide enough for what is relatively simple code.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that it's needed in about half a dozen different places in core. Currently these are using a mixture of different ad-hoc methods to find the version number, and some of these are actually wrong/buggy.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review.

The intention of wp_get_branch_version() here is mainly to consolidate several ad-hoc approaches currently used in core to derive the branch version.

At the moment, different files use different methods such as (float) casting, substr( $ver, 0, 3 ), or preg_split() / explode() logic. Some of these are fragile (for example substr( $ver, 0, 3 ) will break for versions >= 10, and (float) casting can produce incorrect results for x.0 branches).

This PR updates the current call sites that need the branch version to use a shared helper instead:

  • wp-admin/admin-header.php
  • wp-admin/includes/class-core-upgrader.php
  • wp-admin/includes/class-wp-site-health-auto-updates.php
  • wp-admin/includes/plugin-install.php
  • wp-admin/includes/theme.php

The goal is mainly to keep the behavior consistent and avoid repeating slightly different parsing logic across these files.

Happy to adjust the implementation or limit the scope if a smaller change would be preferred.

Copy link
Contributor

@peterwilsoncc peterwilsoncc Mar 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tejas0306 something went wrong last time trunk was merged so the PR is showing 95 changed files instead of the few that have actually changed. Are you able to remerge / rebase so I can follow this up?

…nor version safely.

This introduces a new helper function `wp_get_branch_version()` for extracting the WordPress branch version (major.minor) from a version string.

Currently, several different approaches are used across core to determine the branch version, including:

- `(float)` casting of `get_bloginfo( 'version' )`
- `substr( $ver, 0, 3 )`
- `explode( '.' )` based parsing
- `preg_split()` in `class-core-upgrader.php`

Some of these approaches are fragile or incorrect:

- `(float)` casting can produce incorrect values due to floating-point precision issues.
- `substr( $ver, 0, 3 )` breaks for versions >= 10.
- Multiple inconsistent approaches make maintenance harder.

This patch introduces `wp_get_branch_version()` using a string-based approach:

```php
function wp_get_branch_version( $version = '' ) {
    if ( '' === $version ) {
        $version = wp_get_wp_version();
    }

    $parts = preg_split( '/[.-]/', $version, 3 );

    return $parts[0] . '.' . ( $parts[1] ?? '0' );
}
@tejas0306 tejas0306 force-pushed the add/wp-get-branch-version branch from 60629bf to d584d96 Compare March 16, 2026 06:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants